#include "RingQueue.hpp"
Mutex glock;

void *consumer(void *args)
{
    RingQueue<int> *rq = static_cast<RingQueue<int> *>(args);
    while (true)
    {
        // sleep(5);
        int data = 0;
        rq->Pop(&data);
        {
            LockGuard lockguard(&glock);
            std::cout << "消费了一个数据: " << data << std::endl;
        }
    }
}

void *producter(void *args)
{
    RingQueue<int> *rq = static_cast<RingQueue<int> *>(args);

    int data = 1;
    while (true)
    {
        sleep(1);
        rq->Push(data);
        {
            LockGuard lockguard(&glock);
            std::cout << "生产了一个数据: " << data++ << std::endl;
        }
    }
}

int main()
{
    RingQueue<int> *rq = new RingQueue<int>();
    // pthread_t c, p;
    // pthread_create(&c, nullptr, consumer, (void*)rq);
    // pthread_create(&p, nullptr, producter, (void*)rq);

    // pthread_join(c, nullptr);
    // pthread_join(p, nullptr);

    pthread_t c[2], p[5];

    pthread_create(c + 0, nullptr, consumer, (void *)rq);
    pthread_create(c + 1, nullptr, consumer, (void *)rq);

    pthread_create(p + 0, nullptr, producter, (void *)rq);
    pthread_create(p + 1, nullptr, producter, (void *)rq);
    pthread_create(p + 2, nullptr, producter, (void *)rq);
    pthread_create(p + 3, nullptr, producter, (void *)rq);
    pthread_create(p + 4, nullptr, producter, (void *)rq);

    pthread_join(c[0], nullptr);
    pthread_join(c[1], nullptr);

    pthread_join(p[0], nullptr);
    pthread_join(p[1], nullptr);
    pthread_join(p[2], nullptr);
    pthread_join(p[3], nullptr);
    pthread_join(p[4], nullptr);

    return 0;
}